package com.jinxin.platform.cdperprotypay.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinxin.platform.cdperprotypay.mapper.ChargeRoomDetailMapper;
import com.jinxin.platform.cdperprotypay.mapper.ChargeStandardMapper;
import com.jinxin.platform.cdperprotypay.pojo.bo.SysUser;
import com.jinxin.platform.cdperprotypay.pojo.domain.ChargeRoomDetail;
import com.jinxin.platform.cdperprotypay.pojo.domain.ChargeStandard;
import com.jinxin.platform.cdperprotypay.pojo.form.ChargeStandardForm;
import com.jinxin.platform.cdperprotypay.pojo.vo.ChargeStandardVO;
import com.jinxin.platform.cdperprotypay.service.ChargeStandardService;
import com.jinxin.platform.cdperprotypay.support.ModularSupport;
import org.springframework.stereotype.Service;
import com.jinxin.platform.base.common.pojo.JsonResult;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 收费标准表(ChargeStandard)表服务实现类
 *
 * @author zhangpeng
 * @since 2020-06-28 17:31:04
 */
@Service("chargeStandardService")
public class ChargeStandardServiceImpl implements ChargeStandardService {
    @Resource
    private ChargeStandardMapper chargeStandardMapper;

    @Resource
    private ChargeRoomDetailMapper chargeRoomDetailMapper;

    @Resource
    private ModularSupport modularSupport;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public JsonResult queryById(String id) {
        ChargeStandard chargeStandard = this.chargeStandardMapper.queryById(id);
        return JsonResult.ok(chargeStandard);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param chargeStandard 实例对象
     * @return 对象列表
     */
    @Override
    public JsonResult queryAll(ChargeStandard chargeStandard) {
        List<ChargeStandard> chargeStandardList = this.chargeStandardMapper.queryAll(chargeStandard);
        return JsonResult.ok(chargeStandardList);
    }

    @Override
    public JsonResult pageList(ChargeStandardForm chargeStandardForm) {
        Page<ChargeStandardVO> page = PageHelper.startPage(chargeStandardForm.getPage(), chargeStandardForm.getLimit());
        List<ChargeStandardVO> chargeStandards = chargeStandardMapper.pageList(chargeStandardForm);
        PageInfo<ChargeStandardVO> pageInfo = new PageInfo<>(chargeStandards);
        pageInfo.setPages(page.getPages());
        pageInfo.setTotal(page.getTotal());
        return JsonResult.ok(pageInfo);
    }

    /**
     * 新增数据
     *
     * @param chargeStandard 实例对象
     * @return 实例对象
     */
    @Override
    public JsonResult insert(ChargeStandard chargeStandard) {
        SysUser user = modularSupport.getCurrentUser();
        String userName = "未知";
        if (user != null) {
            userName = user.getUserName();
        }
        String standardNum = "SFBZ" + (new Date().getTime() + "").substring(3, 10);
        chargeStandard.setCreateTime(new Date());
        chargeStandard.setCreater(userName);
        chargeStandard.setStandardNum(standardNum);
        chargeStandard.setStandardStatus("1");
        int i = this.chargeStandardMapper.insert(chargeStandard);
        return i > 0 ? JsonResult.ok("新增成功", chargeStandard) : JsonResult.error("新增失败！");
    }

    /**
     * 修改数据
     *
     * @param chargeStandard 实例对象
     * @return 实例对象
     */
    @Override
    public JsonResult update(ChargeStandard chargeStandard) {
        if (StringUtils.isEmpty(chargeStandard.getItemsId())) { //通过判断字段是否为空确认启用或禁用，否则就是普通修改信息
            int change = isChange(chargeStandard.getId());
            if (change > 0) {
                return JsonResult.error("该收费标准已被关联至收费项中，无法禁用或修改！");
            }
        }
        int i = this.chargeStandardMapper.update(chargeStandard);
        return i > 0 ? JsonResult.ok("修改成功！", null) : JsonResult.error("修改失败！");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public JsonResult deleteById(String id) {
        int change = isChange(id);
        if (change > 0) {
            return JsonResult.error("该收费标准已被关联至收费项中，无法删除！");
        }
        return this.chargeStandardMapper.deleteById(id) > 0 ? JsonResult.ok("删除成功！", null) : JsonResult.error("删除失败！");
    }

    public int isChange(String id) {
        ChargeRoomDetail chargeRoomDetail = new ChargeRoomDetail();
        chargeRoomDetail.setStandardId(id);
        List<ChargeRoomDetail> chargeRoomDetails = chargeRoomDetailMapper.queryAll(chargeRoomDetail);
        return chargeRoomDetails.size();
    }
}